package com.hmall.controller.back;

import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.MallBannerMapper;
import com.hmall.mapper.MallPushMessageMapper;

import com.hmall.mapper.SysLogoMapper;
import com.hmall.model.MallBanner;
import com.hmall.model.MallPushMessage;

import com.hmall.model.SysLogo;
import com.hmall.service.MallBannerService;

import com.hmall.vo.LogoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/manage/banner")
@Api(value = "后台轮播图管理操作API", description = "后台轮播图管理类API说明")
public class BannerManageController {
    private final
    MallBannerMapper mallBannerMapper;
    private final
    MallBannerService mallBannerService;
    private final
    MallPushMessageMapper mallPushMessageMapper;
    private final
    SysLogoMapper sysLogoMapper;

    @Autowired
    public BannerManageController(MallBannerMapper mallBannerMapper, MallBannerService mallBannerService, MallPushMessageMapper mallPushMessageMapper, SysLogoMapper sysLogoMapper) {
        this.mallBannerMapper = mallBannerMapper;
        this.mallBannerService = mallBannerService;
        this.mallPushMessageMapper = mallPushMessageMapper;
        this.sysLogoMapper = sysLogoMapper;
    }

    @AuthRequired
    @PostMapping("CreatedBanner")
    @ApiOperation(value = "添加轮播图")
    public ServerResponse add(@RequestBody
                                      MallBanner mallBanner) {
        System.out.println("新建轮播图:" + mallBanner.toString());

        Date date = new Date();
        mallBanner.setCreateTime(date);
        mallBannerMapper.insert(mallBanner);

        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @PostMapping("GetBannerList")
    @ApiOperation(value = "获取轮播图列表")
    public ServerResponse GetBannerList(
            @RequestParam(value = "pageNum", defaultValue = "1")
                    int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "100")
                    int pageSize) {
        return mallBannerService.getProductList(pageNum, pageSize);
    }

    @AuthRequired
    @PostMapping("DeleteBanner")
    @ApiOperation(value = "删除轮播图")
    public ServerResponse DeleteProduct(@RequestBody
                                                Integer[] id) {
        int product_id;
        int count = 0;

        for (Integer anId : id) {
            product_id = anId;
            count = count + mallBannerMapper.deleteByPrimaryKey(product_id);
        }

        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        } else {
            return ServerResponse.createBySuccess("删除成功");
        }
    }

    @AuthRequired
    @PostMapping("EditBanner")
    @ApiOperation(value = "修改轮播图")
    public ServerResponse EditBanner(@RequestBody
                                             MallBanner mallBanner) {
        System.out.println("更新轮播图:" + mallBanner.toString());

        int count = mallBannerMapper.updateByPrimaryKey(mallBanner);

        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        } else {
            return ServerResponse.createBySuccess("修改成功");
        }
    }

    @AuthRequired
    @PostMapping("GetBanners")
    @ApiOperation(value = "获得产品信息")
    public ServerResponse GetBanner(Integer id) {
        System.out.println("轮播图ID" + id);

        MallBanner banner = mallBannerMapper.selectByPrimaryKey(id);

        return ServerResponse.createBySuccess(banner);
    }

    @AuthRequired
    @GetMapping("SearchBanner")
    @ApiOperation(value = "搜索轮播图")
    public ServerResponse SearchBanner(String query,
                                       @RequestParam(value = "pageNum", defaultValue = "1")
                                               int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "100")
                                               int pageSize) {
        System.out.println(query);

        return mallBannerService.searchBanner(query, pageNum, pageSize);
    }

    @AuthRequired
    @GetMapping("GetRecentNotice")
    @ApiOperation(value = "获取公告")
    public List<MallPushMessage> GetRecentNotice() {
        List<MallPushMessage> noticeList = mallPushMessageMapper.getRecentNotice();
        System.out.println(noticeList);

        return noticeList;
    }

    @AuthRequired
    @GetMapping("GetLogoList")
    @ApiOperation(value = "获取logo列表")
    public ServerResponse GetLogoList(Integer type) {
        List<SysLogo> list = sysLogoMapper.selectByType(type);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @PostMapping("AddLogo")
    @ApiOperation(value = "添加logo")
    public ServerResponse AddLogo(@RequestBody LogoVo logoVo) {
        Integer count = sysLogoMapper.selectCountByType(logoVo.getType());
        if (count != 0) {
            return ServerResponse.createByErrorMessage("该类型已经有logo了，请调用修改接口");
        }
        SysLogo sysLogo = new SysLogo();
        sysLogo.setType(logoVo.getType());
        sysLogo.setLogoUrl(logoVo.getLogoUrl());
        sysLogoMapper.insert(sysLogo);
        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @PostMapping("UpdateLogo")
    @ApiOperation(value = "更新logo")
    public ServerResponse UpdateLogo(@RequestBody LogoVo logoVo) {
        Integer count = sysLogoMapper.selectCountByType(logoVo.getType());
        if (count == 0) {
            return ServerResponse.createByErrorMessage("该类型没有logo，请调用添加接口");
        }
        SysLogo sysLogo = sysLogoMapper.selectLogoByType(logoVo.getType());
        sysLogo.setLogoUrl(logoVo.getLogoUrl());
        sysLogoMapper.updateByPrimaryKey(sysLogo);
        return ServerResponse.createBySuccess("更新成功");
    }

    @AuthRequired
    @PostMapping("DeleteLogo")
    @ApiOperation(value = "删除logo")
    public ServerResponse DeleteLogo(Integer id) {
        sysLogoMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccess("删除成功");
    }

    @GetMapping("GetLogoUrl")
    @ApiOperation(value = "获取logo")
    public ServerResponse GetLogoUrl(Integer type) {
        SysLogo sysLogo = sysLogoMapper.selectLogoByType(type);
        return ServerResponse.createBySuccess(sysLogo);
    }
}
